package ${sys.getPKGCodeName()}.${item.getPSSystemModule().realCodeName.toLowerCase()}.${item.realCodeName.toLowerCase()}

import java.util.List
import org.springframework.data.domain.Page

import ${item.getPKGCodeName()}.${item.realCodeName.toLowerCase()}.dto.*

import net.ibizsys.central.util.annotation.DEDataSet
import net.ibizsys.central.util.annotation.DEAction
import cn.ibizlab.central.plugin.groovy.dataentity.*

/**
 * 实体[${item.name}]运行时对象
 * 此代码用户功能扩展代码
 *
<%if(item.isAdvCustom()) {%>\
 * 该实体存在实体映射或者自定义动态扩展对象，开启RTCode模式会导致映射功能失效
<%}%>\
 */
<%if(item.getPSSysSFPlugin()) {%>\
class ${item.realCodeName} extends ${item.baseEntityRuntime} {
<% } else { %>\
class ${item.realCodeName} extends ${item.baseEntityRuntime}<${item.realCodeName},${item.getDefaultPSDEMethodDTO().realCodeName},${item.getDefaultPSDEFilterDTO().realCodeName}> {
<% } %>\

<% item.getAllPSDEActions().each{ action ->
    if(action.isEnableBackend() && 'BUILTIN' != action.getActionType()) {
%>\
    public static final String ACTION_${action.getName().toUpperCase()} = "${action.getName()}"
<% }} %>\
<% item.getAllPSDEDataSets().each{ dataset ->  %>\
    public static final String DATASET_${dataset.getName().toUpperCase()} = "${dataset.getName()}"
<% } %>\
    private static ${item.realCodeName} _instance
    void setInstance(${item.realCodeName} instance) {
        _instance = instance
    }
    static ${item.realCodeName} getInstance() {
        return _instance
    }

    /**
     * 行为：Create 实际功能
     * @param dto
     * @throws Throwable
     */
    <%= item.isAdvCustom() ? '//' : '' %>@DEAction(ACTION_CREATE)
    ${item.getDefaultPSDEMethodDTO().realCodeName} create(${item.getDefaultPSDEMethodDTO().realCodeName} dto) throws Throwable {
        return this.execute(ACTION_CREATE, dto, ${item.getDefaultPSDEMethodDTO().realCodeName}.class)
    }

    /**
     * 行为：Update 实际功能
     * @param dto
     * @throws Throwable
     */
    <%= item.isAdvCustom() ? '//' : '' %>@DEAction(ACTION_UPDATE)
    ${item.getDefaultPSDEMethodDTO().realCodeName} update(${item.getDefaultPSDEMethodDTO().realCodeName} dto) throws Throwable {
        return this.execute(ACTION_UPDATE, dto, ${item.getDefaultPSDEMethodDTO().realCodeName}.class)
    }

    /**
     * 行为：Remove 实际功能
     * @param keys
     * @throws Throwable
     */
    <%= item.isAdvCustom() ? '//' : '' %>@DEAction(ACTION_REMOVE)
    void remove(${cn.ibizlab.codegen.groovy.util.JavaTypeUtil.getJavaType(item.getKeyPSDEFieldMust().getStdDataType())} key) throws Throwable {
        this.execute(ACTION_REMOVE, key, Void.class)
    }

    /**
     * 行为：Get 实际功能
     * @param key
     * @throws Throwable
     */
    <%= item.isAdvCustom() ? '//' : '' %>@DEAction(ACTION_GET)
    ${item.getDefaultPSDEMethodDTO().realCodeName} get(${cn.ibizlab.codegen.groovy.util.JavaTypeUtil.getJavaType(item.getKeyPSDEFieldMust().getStdDataType())} key) throws Throwable {
        return this.execute(ACTION_GET, key, ${item.getDefaultPSDEMethodDTO().realCodeName}.class)
    }

    /**
     * 行为：GetDraft 实际功能
     * @param dto
     * @throws Throwable
     */
    <%= item.isAdvCustom() ? '//' : '' %>@DEAction(ACTION_GETDRAFT)
    ${item.getDefaultPSDEMethodDTO().realCodeName} getDraft(${item.getDefaultPSDEMethodDTO().realCodeName} dto) throws Throwable {
        return this.execute(ACTION_GETDRAFT, dto, ${item.getDefaultPSDEMethodDTO().realCodeName}.class)
    }

    /**
     * 行为：CheckKey 实际功能
     * @param dto
     * @throws Throwable
     */
    <%= item.isAdvCustom() ? '//' : '' %>@DEAction(ACTION_CHECKKEY)
    int checkKey(${item.getDefaultPSDEMethodDTO().realCodeName} dto) throws Throwable {
        return super.checkKeyState(dto)
    }

    /**
     * 行为：Save 实际功能
     * @param dto
     * @throws Throwable
     */
    <%= item.isAdvCustom() ? '//' : '' %>@DEAction(ACTION_SAVE)
    ${item.getDefaultPSDEMethodDTO().realCodeName} save(${item.getDefaultPSDEMethodDTO().realCodeName} dto) throws Throwable {
        return this.execute(ACTION_SAVE, dto, ${item.getDefaultPSDEMethodDTO().realCodeName}.class)
    }
<%
item.getAllPSDEActions().each {
    if(it.isEnableBackend() && 'BUILTIN' != it.getActionType()) {
%>\

    /**
     * 行为：${it.logicName} 实际功能
     * @param ${it.getPSDEActionInput().inputParams}
     * @throws Throwable
     */
    <%= item.isAdvCustom() ? '//' : '' %>@DEAction(ACTION_${it.name.toUpperCase()})
    def ${it.realCodeName.uncapitalize()}(${it.getPSDEActionInput().input}) throws Throwable {
<% if(it.getActionType() == 'SCRIPT' && (it.scriptCode.startsWith('/*Groovy*/') || it.scriptCode.startsWith('//Groovy'))) { %>\
        /*
        def objRet = { sys,entity ->
            ${it.scriptCode.replaceAll("\n", "\n\t\t\t")}
        }.call(this.getSystemRuntime(), dto)
<% if(it.getPSDEActionReturn().javaType=="void") { %>\
<% } else { %>\
        return objRet
<% } %>\
         */
<%}%>\
<% if(it.getPSDEActionReturn().javaType=="void") { %>\
        this.execute(ACTION_${it.name.toUpperCase()}, ${it.getPSDEActionInput().inputParams}, ${it.getPSDEActionInput().javaType}.class)
<% } else if (it.getPSDEActionReturn().type == "SIMPLES"){ %>\
        return this.executeForList(ACTION_${it.name.toUpperCase()}, ${it.getPSDEActionInput().inputParams}, ${cn.ibizlab.codegen.groovy.util.JavaTypeUtil.getJavaType(it.getPSDEActionReturn().getStdDataType())}.class)
<% } else if (it.getPSDEActionReturn().type == "DTOS"){ %>\
        return this.executeForList(ACTION_${it.name.toUpperCase()}, ${it.getPSDEActionInput().inputParams}, ${it.getPSDEActionReturn().getPSDEMethodDTOMust().getRealCodeName()}.class)
<% } else { %>\
        return this.execute(ACTION_${it.name.toUpperCase()}, ${it.getPSDEActionInput().inputParams}, ${it.getPSDEActionReturn().javaType}.class)
<% } %>\
    }
<%
    }
}
%>\
<%
item.getAllPSDEDataSets().each {
    if(it.isEnableBackend()) {
%>\

    /**
     * 数据集：${it.logicName} 实际功能
     * @param dto
     * @throws Throwable
     */
    <%= item.isAdvCustom()  ? '//' : '' %>@DEDataSet(DATASET_${it.name.toUpperCase()})
    Page<${it.getPSDEDataSetReturn().getPSDEMethodDTOMust().getRealCodeName()}> fetch${it.realCodeName.capitalize()}(${item.getDefaultPSDEFilterDTO().realCodeName} context) throws Throwable {
        return this.fetch(DATASET_${it.name.toUpperCase()}, context, ${it.getPSDEDataSetReturn().getPSDEMethodDTOMust().getRealCodeName()}.class)
    }
<%
    }
}
%>
}